const db = uniCloud.database();
const dbCmd = db.command;
module.exports = {
	_before: function() { // 通用预处理器

	},
	async get() {
		let res =await db.collection("shoppingCarts").where({
			number:dbCmd.neq(0)
		}).get()
		return res
	},
    
	async addCartNumber(user_id, snack_id,value) {
		let a = await db.collection("shoppingCarts").where({
			user_id:user_id,
			snack_id:snack_id
		}).get();
        
		if (a.data.length!=0) {
			let res = await db.collection("shoppingCarts").where({
				user_id,
				snack_id
			}).update({
				number: dbCmd.inc(value)
			})
			return res;
		 }
		 else {
		  let res2= await db.collection("shoppingCarts").add({
				user_id,
				snack_id,	
				number:1,
				checked:true
			})
			return res2;
		}
	},
	
	async deleteShoppingCart(user_id, snack_id) {
		let res=await db.collection("shoppingCarts").where({
				user_id,
				snack_id
			}).remove();
		return res;
	},
	
	async EmptyShoppingCart(user_id,snack_id) {
		let res=await db.collection("shoppingCarts").add({
				user_id,
				snack_id,
				number:0,
				checked:true
			});
		return res;
	}
}